using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Module.CMS.Library;
using NCS.Module.CMS.Library.Distributions;
using NCS.Library.Utilities;
using NCS.Library;
using NCS.Library.Components;

namespace NCS.Module.CMS.Web.HomePage
{
    public partial class ListSpecialNewsInHomePage : CMSWebControl
    {
        //public int zoneid;
        protected int divid = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            //zoneid = ConvertUtility.ToInt32(Portal[Constants.CMS_HomeNew + AppEnv.GetLanguage()]);
            //DataTable other = DistributionController.GetDistributedContents(zoneid, 5, (int)CMSDistributionRank.Special);
            //DataCaching data = new DataCaching();
            //Data.RemoveAll();
            
            DataTable other = DistributionController.Distributions_GetTinNoiBat(6, AppEnv.PortalId()); //DistributionController.GetDistributedHotNewsContentsbyPortal(ConvertUtility.ToInt32(AppEnv.PortalId()), 6);
            if (other.Rows.Count > 0) other.Rows.RemoveAt(0);
            //DataTable mk = other.Clone();
            //if (other.Rows.Count > 0)
            //{
            //    for (int i = 1; i < other.Rows.Count; i++) {
            //        mk.ImportRow(other.Rows[i]);
            //    }
            //}

            //rptContent.DataSource = mk;
            rptContent.DataSource = other;
            rptContent.DataBind();
        }

        protected void rptContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;
                HyperLink hplTitle = (HyperLink)e.Item.FindControl("hplTitle");
                string str = ConvertUtility.ToString(currow["Content_Headline"]);

               // if (hplTitle.Text.Length > 70)
                    hplTitle.Text = Cat_xau( str,65);//hplTitle.Text.Substring(0, 70);

                hplTitle.NavigateUrl = BuildUrlByDistId((int)currow["Distribution_ID"], (int)currow["Distribution_ZoneID"]);
            }
        }


        protected string Cat_xau(string xau, int soluongkytu)
        {
            string xau1 = xau;
            if (xau.Length > soluongkytu)
            {
                xau1 = "";
                // xau.Substring(0, soluongkytu);

                string[] _arr = xau.Split(" ".ToCharArray());
                int count = 0;
                int count_item = 0;
                foreach (string item in _arr)
                {
                    count_item = item.Length;
                    count += count_item + 1;
                    if (count <= soluongkytu)
                    {
                        xau1 += " " + item;
                    }
                    else {
                        break;
                    }
                }
                xau1 += " ...";
            }
            return xau1;
        }
    }
}